package com.example.actordemo1.life;

import akka.actor.*;
import com.example.actordemo1.becom.BecomActor;
import scala.Option;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class LifeParentActor extends AbstractActor {

    Map<String,String> map = new HashMap<>();

    @Override
    public void preStart() throws Exception {

        ActorRef actorRef = getContext().actorOf(Props.create(LifeChildActor.class), "lifeChildActor");

        System.out.println("parentlife  preStart ");
    }

//    @Override
//    public void aroundPreStart() {
//        System.out.println("parentlife  aroundPreStart ");
//    }

    @Override
    public void postStop() throws Exception {
        System.out.println("parentlife  postStop ");
    }

//    @Override
//    public void aroundPostStop() {
//        System.out.println("parentlife  aroundPostStop ");
//    }

//    @Override
//    public void aroundPreRestart(Throwable reason, Option<Object> message) {
//        System.out.println("parentlife  aroundPreRestart ");
//    }

    @Override
    public void preRestart(Throwable reason, Option<Object> message) throws Exception {
        System.out.println("parentlife  preRestart ");
    }

//    @Override
//    public void aroundPostRestart(Throwable reason) {
//        System.out.println("parentlife  aroundPostRestart ");
//    }

    @Override
    public void preRestart(Throwable reason, Optional<Object> message) throws Exception {
        System.out.println("parentlife  preRestart ");
    }

    @Override
    public void postRestart(Throwable reason) throws Exception {
        System.out.println("parentlife  postRestart ");
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder().match(String.class,mag->{
            for (int i = 0;i<2;i++){
                map.put("1"+mag+i,""+mag);
            }
        }).matchAny(o->{
            System.out.println(map);
        }).build();
    }

    public static void main(String[] args) throws InterruptedException {
        ActorSystem actorSystem = ActorSystem.create("sys");

        ActorRef actorRef = actorSystem.actorOf(Props.create(LifeParentActor.class), "lifeParentActor");
        ActorRef actorRef1 = actorSystem.actorOf(Props.create(LifeParentActor.class), "lifeParentActor1");
//        Thread.sleep(3000);
//        System.out.println("1111");
//        ActorSelection actorSelection = actorSystem.actorSelection("/user/lifeParentActor/lifeChildActor");
//        actorSelection.tell("qqq",ActorRef.noSender());
////        actorSystem.stop(actorRef);
////        actorRef.tell(PoisonPill.getInstance(),ActorRef.noSender());
//        actorRef.tell(Kill.getInstance(),ActorRef.noSender());



        actorRef.tell("11",ActorRef.noSender());
        actorRef.tell(11,ActorRef.noSender());
        Thread.sleep(3000);
//        actorRef1.tell("22",ActorRef.noSender());
//        actorRef1.tell(22,ActorRef.noSender());
//        Thread.sleep(3000);
        actorRef.tell("33",ActorRef.noSender());
        actorRef.tell(11,ActorRef.noSender());
    }
}
